package hook

import (
	"os"
	"remoteGo/remote/lvs"
	"remoteGo/remote/service"
)

var master *service.Master
var slave *service.Slave

func ShouldRemote() bool {
	r := len(os.Args) > 1 && os.Args[1] == "slave"
	return !r
}

func BeforeMain() bool {
	if ShouldRemote() {
		master = service.NewMaster(lvs.NewFixedLVS("127.0.0.1:12345"))
		return false
	} else {
		slave = service.NewTCPSlave(12345)
		slave.StartListen()
		select {}
		//return true
	}
}

func AfterGo(uuid service.FuncID, args ...any) bool {
	if ShouldRemote() {
		invoke := master.NewInvokeProcess(uuid, args)
		invoke.Invoke()
		return true
	}
	return false
}
